/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class FileUploadAction extends ActionSupport implements
        ServletRequestAware {

    private File userImage;
    private String userImageContentType;
    private String userImageFileName;
    private HttpServletRequest servletRequest;
    private String successurl;

    public String getSuccessurl() {
        return successurl;
    }

    public File getUserImage() {
        return userImage;
    }

    public void setUserImage(File userImage) {
        this.userImage = userImage;
    }

    public String getUserImageContentType() {
        return userImageContentType;
    }

    public void setUserImageContentType(String userImageContentType) {
        this.userImageContentType = userImageContentType;
    }

    public String getUserImageFileName() {
        return userImageFileName;
    }

    public void setUserImageFileName(String userImageFileName) {
        this.userImageFileName = userImageFileName;
    }

    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;

    }

    @Action(value = "/userImage",
    //    interceptorRefs = {
    //        @InterceptorRef(value = "fileUpload", params = {"allowedType", "image/png,image/gif,image/jpeg,image/pjpeg", "maximumSize", "2097152"})
    //    },
    results = {
        @Result(name = "success", location = "../../WEB-INF/error/SuccessUserImage.jsp"),
        @Result(name = "input", location = "../../fileupload.jsp")
    })
    @Override
    public String execute() {
        successurl = "../../loanapplication.jsp?cat=appli";
        try {

//            String filePath = servletRequest.getRealPath("/");
            String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
            
//            String filePath = servletRequest.getSession().getServletContext().getRealPath(“/”);
//            System.out.println("servlet context: " + ServletActionContext.getRequest().getRealPath("/"));
//            System.out.println("Server path: " + filePath);
//            System.out.println("File name: " + userImageFileName);
//            System.out.println("IMAGE: "+ this.userImage);
//            System.out.println("ETO: " + servletRequest.getRealPath("/"));
            String s = ServletActionContext.getRequest().getRealPath("/");
            String[] split = s.split("\\\\");
            ArrayList<String> path = new ArrayList<String>();
            StringBuffer sb = new StringBuffer();

            for (int i = 0; i <= split.length - 3; i++) {
                path.add(i + " " + split[i]);
                sb.append(split[i]);

                if (i != split.length - 3) {
                    sb.append("\\");
                }
            }
            
            sb.append("\\web\\res\\documentimages") ;
//            C:\Users\Lee\Documents\NetBeansProjects\Cashmate1.2\web\res\documentimages
            String filePath1 = sb.toString();
            System.out.println(sb.toString());

            System.out.println("1: " + getClass().getClassLoader().getResource(".").getPath());
            System.out.println("2: " + ServletActionContext.getRequest().getContextPath());
            System.out.println("3: " + ServletActionContext.getRequest().getRealPath("/"));
            System.out.println("4: " + servletRequest.getContextPath());
            File fileToCreate = new File(filePath, this.userImageFileName);
            File fileToCreate1 = new File(filePath1, this.userImageFileName);

            FileUtils.copyFile(this.userImage, fileToCreate1);
            FileUtils.copyFile(this.userImage, fileToCreate);
        } catch (Exception e) {
            e.printStackTrace();
            addActionError(e.getMessage());

            return INPUT;
        }
        return SUCCESS;
    }
}
//    @Action(interceptorRefs=@InterceptorRef(value="validation",params={"programmatic", "false", "declarative", "true}))
//    <action name="userImage"
//class="net.viralpatel.struts2.FileUploadAction">
//<interceptor-ref name="fileUpload">
//<param name="maximumSize">2097152</param>
// 
//<param name="allowedTypes">
//image/png,image/gif,image/jpeg,image/pjpeg
//</param>
//</interceptor-ref>
//<interceptor-ref name="defaultStack"></interceptor-ref>
//<result name="success">SuccessUserImage.jsp</result>
// 
//<result name="input">UserImage.jsp</result>
//</action>